package com.yyy.instagram;

import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lhh.apst.library.AdvancedPagerSlidingTabStrip;
import com.lhh.apst.library.Margins;
import com.yyy.instagram.common.PullRefreshHeader.MyPullRefreshFrameLayout;
import com.yyy.instagram.fragmnet.NewsMeFragment;
import com.yyy.instagram.fragmnet.NullFragment;

import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by qqqqqqq on 2016/9/28.
 */

public class ThridFragment extends Fragment implements ViewPager.OnPageChangeListener {

    public final int VIEW_SIZE = 2;
    public final int VIEW_FIRST = 0;
    public final int VIEW_SECOND = 1;
    public Fragment mFirstFragment;
    public Fragment mSecondFragment;

    public AdvancedPagerSlidingTabStrip mAPSTS;
    public APSTSViewPager mVP;

    public static ThridFragment instance() {
        ThridFragment view = new ThridFragment();
        return view;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.thrid_fragment, null);
        findViews(view);
        initView();
        return view;
    }

    private void findViews(View view){
        mAPSTS = (AdvancedPagerSlidingTabStrip)view.findViewById(R.id.navbar_tabs);
        mAPSTS.setIndicatorHeight(3);
        mVP = (APSTSViewPager)view.findViewById(R.id.vp_main);
    }

    private void initView(){
        mVP.setOffscreenPageLimit(VIEW_SIZE);
        mVP.setNoFocus(false);
        FragmentAdapter adapter = new FragmentAdapter(getChildFragmentManager());
        mVP.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        mAPSTS.setViewPager(mVP);
        mAPSTS.setOnPageChangeListener(this);
        mVP.setCurrentItem(VIEW_FIRST);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    public class FragmentAdapter extends FragmentStatePagerAdapter {

        public FragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if(position >= 0 && position < VIEW_SIZE){
                switch (position){
                    case  VIEW_FIRST:
                        if(null == mFirstFragment)
                            mFirstFragment = NewsMeFragment.instance();
                        return mFirstFragment;

                    case VIEW_SECOND:
                        if(null == mSecondFragment)
                            mSecondFragment = NewsMeFragment.instance();
                        return mSecondFragment;

                    default:
                        break;
                }
            }
            return null;
        }

        @Override
        public int getCount() {
            return VIEW_SIZE;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if(position == 0) {
                return "关注中";
            }
            if(position == 1){
                return "你";
            }
            return "";
        }
    }
}
